package cn.wenhaha.app.mapping.manager;


import cn.wenhaha.app.mapping.bean.vo.MappingSourceVo;
import cn.wenhaha.app.mapping.bean.vo.MappingVo;
import cn.wenhaha.common.db.bean.SysDataSource;
import cn.wenhaha.common.db.bean.SysMapping;
import cn.wenhaha.common.exception.ApiMSGException;

import javax.annotation.ManagedBean;
import javax.annotation.Resource;
import java.util.Optional;

/**
 * --------
 *
 * @author ：wyndem
 * @Date ：Created in 2022-10-16 21:30
 */
@ManagedBean
public class MappingManager {

    @Resource
    private SysMapping sysMapping;
    @Resource
    private SysDataSource sysDataSource;


    public MappingVo detail(String id) {
        SysMapping sysMapping = Optional.ofNullable(this.sysMapping.findById(id))
                .orElseThrow(() -> ApiMSGException.build(6102));

        MappingSourceVo mappingSource1 = buildMappingSourceVo(
                sysDataSource.findByCode(sysMapping.getSource1Code()),
                sysMapping.getSource1Id(),
                sysMapping.getSource1Name(),
                sysMapping.getSource1Obj(), sysMapping.getSyncCore1());
        MappingSourceVo mappingSource2 = buildMappingSourceVo(
                sysDataSource.findByCode(sysMapping.getSource2Code()),
                sysMapping.getSource2Id(),
                sysMapping.getSource2Name(),
                sysMapping.getSource2Obj(), sysMapping.getSyncCore2());


        return MappingVo.builder()
                .config(sysMapping.getConfig())
                .id(sysMapping.getId())
                .mapping(sysMapping.getMapping())
                .source1(mappingSource1)
                .source2(mappingSource2).build();

    }


    private MappingSourceVo buildMappingSourceVo(SysDataSource s,String id,
                                                 String sourceName, String apiName, String coreName) {
       return MappingSourceVo.builder().id(id)
                .code(s.getCode())
                .icon(s.getIcon())
                .api(apiName)
                .core(coreName)
                .name(sourceName).build();

    }

}
